package com.cloudpivot.receivales.contract.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.contract.entity.erpcontract.*;
import com.cloudpivot.receivales.contract.domain.NormalReturn;
import com.cloudpivot.receivales.contract.domain.oa.OAContract;
import com.cloudpivot.receivales.contract.service.OAService;
import com.cloudpivot.receivales.contract.service.http.HttpAPIServiceForCrm;
import com.cloudpivot.receivales.util.DingMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OAContractServiceImpl implements OAService {

    @Autowired
    private DingMessageUtil dingMessageUtil;

    @Autowired
    private OAEntityToU8EntityImpl oaEntityToU8Entity;

    @Autowired
    private ErpContractServiceImpl erpContractService;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    /**
     * erp写入
     * 事务
     */
    @Transactional(rollbackFor = Exception.class)
    public String inserU8Info(Map<String, Object> inMap) {
        try {
            /**
             * 按照顺序 先contractmain cmlist contractb contractitem
             */
            CMContractMain cmContractMain = (CMContractMain) inMap.get("cmcontractmain");
            CMList cmList = (CMList) inMap.get("cmlist");
            CMContractB cmContractB = (CMContractB) inMap.get("cmcontractb");
            CMContractBExtradefine cmContractBExtradefine = (CMContractBExtradefine) inMap.get("cmcontractbex");
            List<CMContractItemB> cmContractItemBs = (List<CMContractItemB>) inMap.get("cmcontractitems");
            List<CMContractItemBExtradefine> cmContractItemBExtradefines = (List<CMContractItemBExtradefine>) inMap.get("cmcontractitemexs");
            erpContractService.addCMList(cmList);
            erpContractService.addCMContractMain(cmContractMain.getStrContractID());
            erpContractService.addCMContractB(cmContractB);
            erpContractService.addCMContractBExtradeine(cmContractBExtradefine);
            erpContractService.addCMContractItemB(cmContractItemBs);
            erpContractService.addCMContractItemBExtradefine(cmContractItemBExtradefines);
            return "1";
        } catch (Exception ex) {
            ex.printStackTrace();

            dingMessageUtil.pushMessage(null,"插入u8销售合同失败，回滚");
            log.info("插入U8失败,回滚");
            return "-1";
        }
    }

    /**
     * 转换oa请求实体v2 V3共用
     *
     * @param oaContract
     * @return
     */
    @Override
    public NormalReturn getOAPOJO(OAContract oaContract) {
        log.info("获取oa实体,准备转换");
        /*
        根据oa对象 转换为U8
         */
        Map<String, Object> u8map = oaEntityToU8Entity.toConvertEntity(oaContract);

        /**
         * U8插入
         */
        String msg=inserU8Info(u8map);
        String resStr="";
        if ("-1".equals(msg)) {
            resStr = "U8数据库插入合同异常，出现回滚。请查看日志";
            return new NormalReturn(-1, resStr);
        }else{
            resStr =u8map.get("strContractID").toString();
            return new NormalReturn(1, resStr);
        }
    }




    @Override
    public void pushContract(JSONObject contractinfo) {}


}
